/**
 * Copyright (c) 2022 Beijing Jiaotong University
 * PhotLab is licensed under [Open Source License].
 * You can use this software according to the terms and conditions of the [Open Source License].
 * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *
 * See the [Open Source License] for more details.
 *
 * Author: Qing'ao Zhang, Jiang Long, Zhenjie Wei
 * Created: Mar. 15, 2023
 * Supported by: National Key Research and Development Program of China
 */

/* eslint-disable @typescript-eslint/no-explicit-any */

import { PhotComponentPrototype } from "../thrift/gen-nodejs/library_data_type_types"
import { ProjectConfig } from "../thrift/gen-nodejs/project_data_type_types"
import { PhotMaterial } from "../thrift/gen-nodejs/device_data_type_types"
import { PhotFile } from "../thrift/gen-nodejs/file_data_type_types"
import thriftClient from "../utils/conn"
import service from "../thrift/gen-nodejs/Library"

export default class LibraryService {
    private _client: any
    constructor(socket: [string, number]) {
        this._client = thriftClient(service, socket[0], socket[1])
    }

    /**
     * Get the list of component libraries.
     * @param path - The relative path of the component library, default is "./"
     * @returns list<PhotFile>: File List
     */
    public async getLibraryList(path: string): Promise<PhotFile[]> {
        return await this._client.get_library_list(path)
    }

    /**
     * Get a component prototype.
     * @param path - The relative path of the component in the component library.
     * @returns PhotComponentPrototype: Component prototype.
     */
    public async getComponentPrototype(path: string): Promise<PhotComponentPrototype> {
        return await this._client.get_component_prototype(path)
    }

    /**
     * Save a component prototype.
     * @param path - The relative path of the component in the component library.
     * @param prototype - Component prototype.
     * @returns null.
     */
    public async saveComponentPrototype(path: string, prototype: PhotComponentPrototype) {
        return await this._client.save_component_prototype(path, prototype)
    }

    /**
     * Delete a component prototype.
     * @param path - The relative path of the component in the component library.
     * @returns null.
     */
    public async deleteComponentPrototype(path: string) {
        return await this._client.delete_component_prototype(path)
    }

    /**
     * Search component, the search scope includes component name (name) and component description (description). 
     * Other search functions are not supported at the moment.
     * @param queryString - Query statement.
     * @returns list<PhotComponentPrototype>: Component prototype list.
     */
    public async searchComponent(queryString: string): Promise<PhotComponentPrototype[]> {
        return await this._client.search_component(queryString)
    }

    public async getSampleList(relativePath: string): Promise<ProjectConfig[]> {
        return await this._client.get_sample_list(relativePath)
    }

    public async getMaterialList(): Promise<PhotMaterial[]> {
        return await this._client.get_material_list()
    }

    public async getIconList(): Promise<string[]> {
        return await this._client.get_icon_list()
    }
}
